package com.kmmlearn.findtime.android.ui

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import com.kmmlearn.findtime.android.theme.MyApplicationTheme
import com.kmmlearn.findtime.android.ui.Screen.FindTimeScreen

sealed class Screen(val title: String) {
    object TimeZoneScreen : Screen("TimeZones")
    object FindTimeScreen : Screen("FindTime")
    object AnimatedScreen : Screen("Animated")
}

data class BottomNavigationItem(
    val route: String,
    val icon: ImageVector,
    val iconContentDescription: String
)

val bottomNavigationItems = listOf(
    BottomNavigationItem(
        route = Screen.TimeZoneScreen.title,
        icon = Icons.Filled.Home,
        iconContentDescription = "TimeZones"
    ),
    BottomNavigationItem(
        route = Screen.FindTimeScreen.title,
        icon = Icons.Filled.Search,
        iconContentDescription = "FindTime"
    ),
    BottomNavigationItem(
        route = Screen.AnimatedScreen.title,
        icon = Icons.Filled.Favorite,
        iconContentDescription = "Animated"
    )
)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainView(actionBarFun: topBarFun = { emptyComposable() }) {
    // remember 确保状态在重组时保持不变
    //mutableStateOf 提供状态的可变性
    //两者结合提供持久的、可观察的可变状态
    val showAddDialog = remember { mutableStateOf(false) }
    val currentTimezonesStrings = remember { SnapshotStateList<String>() }
    // For Test
//    currentTimezonesStrings.add("America/New_York")
    val selectedIndex = remember { mutableIntStateOf(0) }
    MyApplicationTheme {
        Scaffold(
            topBar = {
                actionBarFun(selectedIndex.intValue)
            },
            floatingActionButton = {
                if (selectedIndex.intValue == 0) {
                    FloatingActionButton(
                        modifier = Modifier.padding(16.dp),
                        onClick = {
                            showAddDialog.value = true
                        }
                    ) {
                        Icon(
                            imageVector = Icons.Filled.Add,
                            contentDescription = null
                        )
                    }
                }
            },
            bottomBar = {
                NavigationBar {
                    bottomNavigationItems.forEachIndexed { index, item ->
                        NavigationBarItem(
                            icon = {
                                Icon(
                                    imageVector = item.icon,
                                    contentDescription = item.iconContentDescription
                                )
                            },
                            selected = selectedIndex.intValue == index,
                            onClick = {
                                selectedIndex.intValue = index
                            }
                        )
                    }
                }
            }
        ) { innerPadding ->
            // TODO: Replace with Dialog
            if (showAddDialog.value) {
                AddTimeZoneDialog(
                    onAdd = {
                        newTimezones ->
                        showAddDialog.value = false
                        for (zone in newTimezones) {
                            if (!currentTimezonesStrings.contains(zone)) {
                                currentTimezonesStrings.add(zone)
                            }
                        }
                    },
                    onDismiss = {
                        showAddDialog.value = false
                    }
                )
            }


            Box(modifier = Modifier.padding(innerPadding)) {
                when (selectedIndex.intValue) {
                    0 -> TimeZoneScreen(currentTimezonesStrings)
                    1 -> FindMeetingScreen(currentTimezonesStrings)
                    2 -> AnimatedScreen()
                }
            }
        }
    }

}







